var app = getApp();
Page({
  data: {
    checkList: [
      { id: 'M0', disabled: true },
      { id: 'D0' },
      { id: 'M1' },
      { id: 'D1' },
      { id: 'M2' },
      { id: 'D2' },
      { id: 'M3' },
      { id: 'D3' },
      { id: 'M4' },
      { id: 'D4' },
      { id: 'M4' },
      { id: 'D5' }
    ],
    dateList: [
      {
        dateName: '前一天'
      }, {
        dateName: '第一天'
      }, {
        dateName: '第二天'
      }, {
        dateName: '第三天'
      }, {
        dateName: '第四天'
      }, {
        dateName: '第五天'
      }
    ],
    user: [],
    is_bind: false,
    zhusu: ['空', '不住', '单住', '合住'],
    zhusuValue: 0,
    xingbie: ['男', '女'],
    xingbieValue: 0,
    shopid: [],
    shopidValue: 0,
    shopname: [],
    shopnameValue: 0,
    pian: [],
    pianValue: 0,
    sheng: [],
    shengValue: 0,
    shi: [],
    shiValue: 0,
    sp_id: '',
    sp_name: '',
    spname: '',
    Gray: '',
    server: app._server,
    days: 0,
    meal: [],
    indate: '',
    outdate: ''
  },
  onLoad: function (options) {
    var sp_id = options.ID.split(',')[0];
    var spname = options.ID.split(',')[1] + '(第' + options.ID.split(',')[2] + '期)';
    wx.setNavigationBarTitle({
      title: spname
    })
    this.setData({
      sp_id: sp_id,
      spname: spname,
      user: app._user,
      days: options.ID.split(',')[3]
    });
    this.GetAllUser();
  },
  GetAllUser: function () {
    var that = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'Getstr'
      },
      success: (res) => {
        wx.hideToast();
        that.setData({
          pian: res.data.Pian
        })
        wx.hideToast();
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    });
  },
  formBindsubmit: function (e) {
    var _this = this;
    var formData = e.detail.value;
    formData.sp_id = _this.data.sp_id;
    formData.openid = _this.data.user.openid;
    formData.meal = _this.data.meal;
    formData.method = 'signup_hz';
    var Pian = e.detail.value.pian,
      Sheng = e.detail.value.sheng,
      Shi = e.detail.value.shi,
      ShopName = e.detail.value.shopname,
      ShopID = e.detail.value.shopid,
      name = e.detail.value.name,
      xingbie = e.detail.value.xingbie,
      Zhiwei = e.detail.value.zhiwei,
      Phone = e.detail.value.phone,
      Zhusu = e.detail.value.zhusu,
      indate = e.detail.value.indate,
      outdate = e.detail.value.outdate;
    if (!Pian || Pian == '空') {
      app.showErrorModal('片区不能为空', '提醒');
      return false;
    }
    if (!Sheng || Sheng == '空') {
      app.showErrorModal('省份不能为空', '提醒');
      return false;
    }
    if (!Shi || Shi == '空') {
      app.showErrorModal('市区不能为空', '提醒');
      return false;
    }
    if (!ShopName || ShopName == '空') {
      app.showErrorModal('店简称不能为空', '提醒');
      return false;
    }
    if (!ShopID || ShopID == '空') {
      app.showErrorModal('店代码不能为空', '提醒');
      return false;
    }
    if (!name) {
      app.showErrorModal('姓名不能为空', '提醒');
      return false;
    }
    if (!xingbie || xingbie == '空') {
      app.showErrorModal('性别不能为空', '提醒');
      return false;
    }
    if (!Zhiwei) {
      app.showErrorModal('职位不能为空', '提醒');
      return false;
    }
    if (!Phone) {
      app.showErrorModal('电话不能为空', '提醒');
      return false;
    }
    if (!Zhusu || Zhusu == '空') {
      app.showErrorModal('住宿不能为空', '提醒');
      return false;
    }
    if (Zhusu == '单住' || Zhusu == '合住') {
      if (!indate || indate == '') {
        app.showErrorModal('入住时间不能为空', '提醒');
        return false;
      }
      if (!outdate || outdate == '') {
        app.showErrorModal('退房不能为空', '提醒');
        return false;
      }
    }
    if (indate > outdate) {
      app.showErrorModal('退房时间不能大于入住时间', '提醒');
      return false;
    }

    //if ((this.data.sp_name.indexOf('总经理') >= 0 && Zhiwei.indexOf('总经理') < 0) || (this.data.sp_name.indexOf('服务经理') >= 0 && Zhiwei.indexOf('服务经理') < 0)) {
    //  app.showErrorModal('您的职位与报名项目不符', '提醒');
    //  return false;
    //}
    console.log(formData)
    app.showLoadToast('连接中');
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: formData,
      success: function (res) {
        wx.hideToast();
        if (res.data.insertsignup) {
          wx.showModal({
            title: '提示',
            content: '报名成功',
            showCancel: false,
            success: function (res) {
              _this.GetAllUser();
              wx.switchTab({
                url: '/pages/mine/mine'
              })
            }
          });
        }
        else {
          app.showErrorModal(res.data.error, '报名失败');
        }
      },
      fail: function (res) {
        app.showErrorModal(res.errMsg, '绑定失败');
      }
    });
  },
  clickScale: function (e) {
    var current = e.target.dataset.index
    wx.previewImage({
      urls: [current]
    })
  },
  zhusuchange: function (e) {
    this.setData({
      zhusuValue: e.detail.value
    })
  },
  xingbiechange: function (e) {
    this.setData({
      xingbieValue: e.detail.value
    })
  },
  Change_pian: function (e) {
    if (e.detail.value == '0') {
      return;
    }
    this.setData({
      pianValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetPian',
        Pian: _this.data.pian[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          sheng: res.data.Sheng
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_sheng: function (e) {
    if (e.detail.value == '0') {
      return;
    }
    this.setData({
      shengValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetSheng',
        Sheng: _this.data.sheng[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shi: res.data.Shi
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_shi: function (e) {
    if (e.detail.value == '0') {
      return;
    }
    this.setData({
      shiValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetShi',
        Shi: _this.data.shi[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shopname: res.data.ShopName
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_shopname: function (e) {
    if (e.detail.value == '0') {
      return;
    }
    this.setData({
      shopnameValue: e.detail.value
    })
    var _this = this;
    wx.request({
      url: app._server + '/Weixin_Hodna/WxLogin.ashx',
      data: {
        method: 'GetShopName',
        ShopName: _this.data.shopname[e.detail.value]
      },
      success: (res) => {
        wx.hideToast();
        _this.setData({
          shopid: res.data.ShopID
        })
      },
      fail: function (res) {
        wx.hideToast();
        app.showErrorModal(res.errMsg);
      }
    })
  },
  Change_shopid: function (e) {
    this.setData({
      shopidValue: e.detail.value
    });
  },
  checkboxChange: function (e) {
    this.setData({
      meal: e.detail.value
    });
  },
  Change_indate: function (e) {
    this.setData({
      indate: e.detail.value
    })
  },
  Change_outdate: function (e) {
    this.setData({
      outdate: e.detail.value
    })
  },
})